
#include <iostream>
#include <string>

// 抽象类：定义饮料制作流程
class Beverage {
public:
    virtual ~Beverage() = default;

    // 模板方法：定义制作流程
    void prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        if (customerWantsCondiments()) 
        {
            addCondiments();
        }
    }

protected:
    // 基本步骤
    void boilWater() const {
        std::cout << "Boiling water\n";
    }

    void pourInCup() const {
        std::cout << "Pouring into cup\n";
    }

    // 抽象方法：由子类实现
    virtual void brew() const = 0;
    virtual void addCondiments() const = 0;

    // 钩子方法：子类可选择重写
    virtual bool customerWantsCondiments() const {
        return true;
    }
};

// 具体类：咖啡
class Coffee : public Beverage {
protected:
    void brew() const override {
        std::cout << "Dripping coffee through filter\n";
    }

    void addCondiments() const override {
        std::cout << "Adding sugar and milk\n";
    }

    bool customerWantsCondiments() const override {
        std::cout << "Do you want sugar and milk with your coffee (y/n)? ";
        char answer;
        std::cin >> answer;
        return (answer == 'y' || answer == 'Y');
    }
};

// 具体类：茶
class Tea : public Beverage {
protected:
    void brew() const override {
        std::cout << "Steeping the tea\n";
    }

    void addCondiments() const override {
        std::cout << "Adding lemon\n";
    }

    bool customerWantsCondiments() const override {
        std::cout << "Do you want lemon with your tea (y/n)? ";
        char answer;
        std::cin >> answer;
        return (answer == 'y' || answer == 'Y');
    }
};

// 客户端代码
int main() {
    
    std::cout << "Making coffee:\n";
    Coffee coffee;
    coffee.prepareRecipe();

    std::cout << "\nMaking tea:\n";
    Tea tea;
    tea.prepareRecipe();

    return 0;
}